#include <stdio.h>

void delay( int time )
{
	int i, j;
	
	for( i = 0; i < time; i++ )
		for( j = 0; j < 65535; j++ )
			;	
}

void handler1( void )
{
	printf( "No1 exception occured ! \n" );
	
	return;	
}

void handler2( void )
{
	printf( "No2 exception occured ! \n" );
	
	return;	
}

void install_swi_handler( void(*pf)(void) )
{	
	int * p_handler_addr;
	int * p_swi_addr;
	int bounce_instruction;
	
	p_handler_addr = (int *)0x58;	
	*p_handler_addr = (int)pf;
	
	p_swi_addr = (int *)0x8;	
	bounce_instruction = 0xe59ff000 | ((int)p_handler_addr - (int)p_swi_addr - (int)0x8);
	
	*p_swi_addr = bounce_instruction;

	return;
}

void swi_handler(void);

int main( void )
{
	int i = 0;	
	
	/* change to user mode */
	__asm
	{
		mov r0, 0xd0
		MSR CPSR_cxsf, r0
	}
		
	install_swi_handler( swi_handler );
	
	//p = (int *)0x8;	
	//*p = 0xEA002026;
		
	printf( "hello, world \n" );
	
	/* main loop */
	while( i < 1000 )
	{
		printf( "current i = %d \n", i );
		
		delay( 10 );
		
		i++;	
		
		if( i % 10 == 0 )
		{
			/* call swi */
			__asm
			{					
				 	swi 0x0
			}			
		}
	}
		
	return 0;
}